package com.see.domain;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.see.exception.MessegeFeedException;

public class MessegeParser {
	public static final String FULLMSG = "FULLMSG";

	private String msg;

	public MessegeParser(final String line) {
		this.msg = line;
	}

	public HashMap<String, String> parseMsg() throws MessegeFeedException {
	         
         
		HashMap<String, String> paramMap = new HashMap<>();
		for (MsgFields param : MsgFields.values()) {
			Pattern pattern = Pattern.compile(param.pattern);
			Matcher matcher = pattern.matcher(msg);
			if (matcher.lookingAt()) {
				param.value = matcher.group(1);
				paramMap.put(param.name(), param.value);
			}
			paramMap.put(FULLMSG, msg);
		}
		return paramMap;
	}

	public static enum MsgFields {
		TYPEMSG(".*typemsg=(\\w*)|"), 
		USERNAME(".*username=(\\w*)|"), 
		PRODUCT(".*product=(\\w*)|"),
		AMOUNT(".*amount=([\\d\\.]+)|"),
		PRICE(".*price=([\\d\\.]+)|"),
		MARKET(".*market=([12])|"),
		ERROR(".*error=(\\w*)|"),
		SUCCESS(".*success=(\\w*)|"),
		ORDERID(".*orderid=([\\d\\.]+)|"),
		ORDERTYPE(".*ordertype=([12])|"),
		ORDERMATCHPRICE(".*ordermatchprice=(\\w*)|"),
		ORDERCOUNTERPARTY(".*ordercounterparty=(\\w*)|");
		private String pattern;
		public  String value;

		private MsgFields(String param) {
			this.pattern = param;
		}
	}
	
	 
}
